import store from '@/store'
import { getMousePos } from '@/plugins/utils'
import { H5_TYPE } from '@/common/const-dif'
import { PAGE_TYPE } from '@/common/const'
import { EventBus } from '@/store/index'
import { postStatics } from '@/service/statics'
import { Toast } from 'vant'
import { nextTick } from 'vue'

export default class BaseEvent {
  constructor() {
    this.item;
    this.event;
    this.isH5Edit = false;
    this.store = store;
  }

  get commonAttr() {
    return this.item.commonAttr;
  }
  get componentMap() {
    return this.store.state.componentMap;
  }
  get canvasImgComponentMap() {
    return this.store.state.canvasImgComponentMap;
  }
  get controls() {
    return this.store.state.controls ? this.store.state.controls : this.store.state.wbComponents.controls;
  }
  get allPages() {
    return this.store.state.allPages ? this.store.state.allPages : this.store.state.components.controls;
  }
  get siteInfo() {
    return this.store.state.siteInfo;
  }
  get interactionData() {
    return this.store.state.interactionData;
  }
  get frontData() {
    return this.store.state.frontData;
  }
  get formValues() {
    return this.store.state.formValues;
  }
  get globalIsPost() {
    return this.store.state.globalIsPost;
  }
  get pages() {
    return this.store.state.pages;
  }
  get pops() {
    return this.store.state.pops_;
  }
  get statePopId() {
    return this.store.state.popId;
  }
  get pageIndex() {
    return this.store.state.pageIndex;
  }
  get siteAttrs() {
    return this.store.state.attrs;
  }

  get appid() {
    return this.store.state.attrs.wb_appkey;
  }
  get uid() {
    return this.store.state.userInfo.uid;
  }
  get userInfo() {
    return this.store.state.userInfo;
  }
  get push_visible() {
    return this.store.state.attrs.push_visible;
  }
  get isIPhone() {
    return this.store.state.md.isIPhone;
  }

  get bindData() {
    return this.store.state.bindData;
  }
  get activityInfo() {
    return this.store.state.activityInfo;
  }

  excute({item, event}) {
    this.item = item;
    this.event = event;
  }

  get getItemControls() {
    if (this.isH5Edit) {
      return this.controls[this.item.id]
    } else {
      if (this.item) {
        return this.controls[this.item.id]
      } else {
        return null
      }
    }
  }

  openLinkBySelfUrl(jumpUrl) {
    let blocked = false;
    try {
      let worxWin = window.open(jumpUrl);
      if (worxWin == null) {
        blocked = true;
      }
    } catch (ex) {
      blocked = true;
    }
    if (blocked) {
      window.location.href = jumpUrl
    }
  }



  getMainWbDataEventByEvents(events) {
    let event_
    let tempArr = []
    _.forEach(events, (event) => {
      if (event.controlId && event.controlId.length > 0) {
        tempArr.push(event)
      }
    })
    if (tempArr.length > 0) {
      tempArr = _.sortBy(tempArr, (event) => {
        return event.index;
      });
      event_ = tempArr[0]
      //主事件是组件的第一个事件，是wblist值， controlId是组件的主事件id
    }
    return event_
  }

  showToast(msg) {
    if(_.trim(msg)){
      Toast(msg);
    }
  }

  showPop(popId) {
    if(!this.pops[popId-1]){
      return
    }
    if (this.statePopId > 0 && this.statePopId != popId) {
      EventBus.$emit('closePop')
    }
    this.store.commit('updatePopId', { popId })
    EventBus.$emit("showPop", { popId })
  }

  showPage(pageId) {
    if(!this.pages[pageId-1]){
      return
    }
    if (this.statePopId > 0) {
      EventBus.$emit('closePop')
    }

    EventBus.$emit("isStartHtml2canvas", { isStart: true, compId: this.item.id});
    let pageIndex = pageId - 1
    this.store.commit('updatePageIndex', { pageIndex })

    EventBus.$emit('pauseVideo', { pageId })
    EventBus.$emit('playOrPauseAudio', { isPlay: false })
    if (this.siteAttrs.h5Type == H5_TYPE.mobile && !this.siteAttrs.isNoSwiping && this.siteAttrs.pageType == PAGE_TYPE.single) {
      EventBus.$emit("swiper", { pageId })
    } else {
      EventBus.$emit('showPage', { pageId })
    }
    nextTick(() => {
      EventBus.$emit("isStartHtml2canvas", { isStart: false, compId: this.item.id});
    })
  }

  onPostStatics(e, { comType, wModule, jumpUrl, params, apiUri }) {
    if (this.isH5Edit) {
      return
    }
    let xy = getMousePos(e)
    let comId
    if (this.item) {
      comId = this.item.id
    } else {
      comType = 'pageOrPop'
      comId = "pageId: " + this.pageId + "popId: " + this.popId
    }
    postStatics(this.store.state, {
      comType, comId, wModule, pageIndex: this.pageIndex, apiType: "",
      apiUri: apiUri ? apiUri : "", params: params ? params : "", jumpUrl, clickSequence: "", mouseX: xy.x, mouseY: xy.y
    })
  }

  destroy() {
    this.item = null;
    this.event = null;
    this.isH5Edit = false;
  }
}
